package com.yicong.taxi.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yicong.taxi.api.Api;
import com.yicong.taxi.api.ApiClient;
import com.yicong.taxi.api.observers.EntryResultObserver;
import com.yicong.taxi.base.BaseActivityPresenter;
import com.yicong.taxi.config.MyApp;
import com.yicong.taxi.iview.LoginView;
import com.yicong.taxi.model.MsgAuth;
import com.yicong.taxi.utils.CommonUtil;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class LoginPresenter extends BaseActivityPresenter<LoginView> {
    public LoginPresenter(Activity activity, LoginView iView,
                          LifecycleProvider<ActivityEvent> provider) {
        super(activity, iView, provider);
    }

    public void sendMsg(String msgType,String phone,String msgContent,String remark){
        Map<String, Object> map = new LinkedHashMap<>();//body  map
        map.put("db",MyApp.db);
        map.put("function","app_insert_short_message");
        map.put("login_id","0");
        map.put("message_cls",msgType);
        map.put("tel",phone);
        map.put("message_content",msgContent);
        map.put("remark",remark);
        String json= new Gson().toJson(map);//要传递的json
        RequestBody requestBody= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ApiClient.getApiRetrofitInstance()
                .sendMsg(requestBody)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(provider.bindToLifecycle())
                .subscribe(new EntryResultObserver<MsgAuth>(activity) {
                    @Override
                    public void onSuccess(MsgAuth msgAuth) {
                        if(iView!=null){
                            iView.registerSuccess(msgAuth);
                        }
                    }
                });
    }
}
